<?php
$g4_path = '../../';
require_once ($g4_path.'lib.php');
require_once ($g4_path.'common.php');
require_once ('../admin.lib.php');

header("Content-Type: text/html; charset=UTF-8");

$cate1 = $_REQUEST['cate1'];
$cate2 = $_REQUEST['cate2'];
$cate3 = $_REQUEST['cate3'];
$event_id = $_REQUEST['event_id'];

$target_num = substr($_POST['original_target_event'], -1, 1); 
$_sqlWhere = NULL;

        if($cate1) {
            $_sqlWhere.= " cate1 = '".$cate1."' "; 
        }
        if($cate2 && $target_num > 1) {
            if($_sqlWhere) {
                $_sqlWhere.= ' AND '; 
            }
            $_sqlWhere.= " cate2 = '".$cate2."' "; 
        }
        if($cate3 && $target_num > 2) {
            if($_sqlWhere) {
                $_sqlWhere.= ' AND '; 
            }
            $_sqlWhere.= " cate3 = '".$cate3."' "; 
        }

        if($_sqlWhere) {
            $_sqlWhere = ' WHERE '.$_sqlWhere; 
        }
        
$_explain = "--선택해주세요--";
$_nodata = "--하위 분류가 없습니다--";

switch($event_id) {
        case 'cate1':
        $_catesql ='SELECT cate2 v FROM book_category '.$_sqlWhere.' GROUP BY cate2';
        $change_id = 'cate2';
        break;

        case 'cate2':
        $_catesql ='SELECT cate3 v FROM book_category '.$_sqlWhere.' GROUP BY cate3';
        $change_id = 'cate3';
        break;

        case 'cate3':
            $change_id = 'cate4';
            $_catesql="SELECT name as v, no as n FROM book ".$_sqlWhere;
            $_explain = "--교재 선택이 가능합니다--";
            $_nodata = "--교재가 없습니다--";
        break;
}

$list = fetchAll2($_catesql);
array_unshift($list, array('',$_explain));

if(sizeof($list) == 1) {
        echo json_encode2(array('change_id'=>$change_id,'list'=>array(array('',$_nodata))));
        exit;
}

if($change_id != '') {
        echo json_encode2(array('change_id'=>$change_id,'list'=>$list));
}
